package com.yswl.yeeduo.ui.db;


import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yswl.yeeduo.ui.bean.WXUserInfo;

public class WxUserInfoDao extends BaseDao {

//	private static final String TABLE_NAME = "tbl_ca";
//	private static final String ID = "id";
//	private static final String CA_NAME = "ca_name";
//	private static final String CA_INFO = "ca_info";
//	private static final String USER_NAME = "user_name";
//	private static final String USER_INFO = "user_info";
	
	private static final String TABLE_NAME = "WXUser_table";
	private static final String ID = "Id";
	
	private static final String NAME = "nickname";
	private static final String SEX = "sex";
	private static final String PROVINCE = "province";
	private static final String CITY = "city";
	private static final String COUNTRY = "country";
	private static final String HEAD = "headimgurl";

	private static WxUserInfoDao share;
	public static WxUserInfoDao getInstance(){
		if (share == null) {
			share = new WxUserInfoDao();
		}
		return share;
	} 
	private WxUserInfoDao() {
		super();
	}
	
	public static String CREATE_SQL() {
		return "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + ID + " integer primary key autoincrement," + NAME + "," + HEAD + "," + COUNTRY + ","+ CITY + ","+ PROVINCE + "," + SEX + "  integer)";
	}

	public void insert(WXUserInfo info) {
		SQLiteDatabase db = this.getDatabase(true);
		String sql = "INSERT INTO " + TABLE_NAME + " (" + NAME + " ," + SEX + "," + PROVINCE + "," + CITY + ","+ COUNTRY + ","+ HEAD + ") VALUES (?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { info.nickname, info.sex, info.province, info.city,info.country ,info.headimgurl });
		closeDatabase(db);
	}

//	public void deleteById(int caId) {
//		SQLiteDatabase db = this.getDatabase(true);
//		String sql = "DELETE FROM " + TABLE_NAME + " WHERE " + ID + "=?";
//		db.execSQL(sql, new Object[] { caId });
//		closeDatabase(db);
//	}

//	public void deleteByName(String name) {
//		SQLiteDatabase db = this.getDatabase(true);
//		String sql = "DELETE FROM " + TABLE_NAME + " WHERE " + NAME + "=?";
//		db.execSQL(sql, new Object[] { name });
//		closeDatabase(db);
//	}

	/**
	 *  从数据库中得到CA证书信息 列表
	 */
	public ArrayList<WXUserInfo> queryAllUser() {
		SQLiteDatabase db = this.getDatabase(false);
		ArrayList<WXUserInfo> returnList = new ArrayList<WXUserInfo>();
		String sql = "SELECT * FROM " + TABLE_NAME;
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			WXUserInfo userInfo = new WXUserInfo();
			userInfo.nickname = cursor.getString(cursor.getColumnIndex(NAME));
			userInfo.sex = cursor.getInt(cursor.getColumnIndex(SEX));
			userInfo.province = cursor.getString(cursor.getColumnIndex(PROVINCE));
			userInfo.city = cursor.getString(cursor.getColumnIndex(CITY));
			userInfo.country = cursor.getString(cursor.getColumnIndex(COUNTRY));
			userInfo.headimgurl = cursor.getString(cursor.getColumnIndex(HEAD));
			returnList.add(userInfo);
		}
		closeCursor(cursor);
		closeDatabase(db);
		return returnList;
	}

//	public WXUserInfo queryById(int bid) {
//		SQLiteDatabase db = this.getDatabase(false);
//		WXUserInfo ca = null;
//		String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " + ID + "=?";
//		Cursor cursor = db.rawQuery(sql, new String[] { bid + "" });
//		if (cursor.moveToFirst()) {
//			ca = new WXUserInfo();
//			ca.id = cursor.getInt(cursor.getColumnIndex(ID));
//			ca.caName = cursor.getString(cursor.getColumnIndex(CA_NAME));
//			ca.userInfo = cursor.getString(cursor.getColumnIndex(USER_INFO));
//			ca.caInfo = cursor.getString(cursor.getColumnIndex(CA_INFO));
//			ca.userName = cursor.getString(cursor.getColumnIndex(USER_NAME));
//		}
//		closeCursor(cursor);
//		closeDatabase(db);
//		return ca;
//	}

	public ArrayList<WXUserInfo> queryByName(String name) {
		SQLiteDatabase db = this.getDatabase(false);
		String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " + NAME + " =?";
		Cursor cursor = db.rawQuery(sql, new String[] { name });
		ArrayList<WXUserInfo> returnList = new ArrayList<WXUserInfo>();
		if (cursor.moveToFirst()) {
			WXUserInfo userInfo = new WXUserInfo();
			userInfo.nickname = cursor.getString(cursor.getColumnIndex(NAME));
			userInfo.sex = cursor.getInt(cursor.getColumnIndex(SEX));
			userInfo.province = cursor.getString(cursor.getColumnIndex(PROVINCE));
			userInfo.city = cursor.getString(cursor.getColumnIndex(CITY));
			userInfo.country = cursor.getString(cursor.getColumnIndex(COUNTRY));
			userInfo.headimgurl = cursor.getString(cursor.getColumnIndex(HEAD));
			returnList.add(userInfo);
		}
		closeCursor(cursor);
		closeDatabase(db);
		return returnList;
	}

	public boolean isExistCa(String name) {
		List<WXUserInfo> list = new WxUserInfoDao().queryByName(name);
		if (list.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}
}
